package com.example.excelimporter.controller;

import com.example.excelimporter.model.ListenerConfig;
import com.example.excelimporter.model.TemplateConfig;
import com.example.excelimporter.service.TemplateConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/templates")
@Tag(name = "Template Configuration", description = "API for managing Excel import templates")
public class TemplateConfigController {

    @Autowired
    private TemplateConfigService templateConfigService;

    @PostMapping
    @Operation(summary = "Create template", description = "Create a new Excel import template")
    @ApiResponse(responseCode = "200", description = "Template created successfully")
    @ApiResponse(responseCode = "400", description = "Invalid template configuration")
    public TemplateConfig createTemplate(
            @Parameter(description = "Template configuration details") @RequestBody TemplateConfig templateConfig) {
        validateListeners(templateConfig);
        return templateConfigService.createTemplateConfig(templateConfig);
    }

    @PutMapping("/{id}")
    @Operation(summary = "Update template", description = "Update an existing Excel import template")
    @ApiResponse(responseCode = "200", description = "Template updated successfully")
    @ApiResponse(responseCode = "404", description = "Template not found")
    public TemplateConfig updateTemplate(
            @Parameter(description = "Template ID") @PathVariable String id,
            @Parameter(description = "Updated template configuration") @RequestBody TemplateConfig templateConfig) {
        validateListeners(templateConfig);
        return templateConfigService.updateTemplateConfig(id, templateConfig);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "Delete template", description = "Delete an Excel import template")
    @ApiResponse(responseCode = "200", description = "Template deleted successfully")
    @ApiResponse(responseCode = "404", description = "Template not found")
    public void deleteTemplate(
            @Parameter(description = "Template ID") @PathVariable String id) {
        templateConfigService.deleteTemplateConfig(id);
    }

    @GetMapping("/{id}")
    @Operation(summary = "Get template", description = "Retrieve an Excel import template by ID")
    @ApiResponse(responseCode = "200", description = "Template retrieved successfully")
    @ApiResponse(responseCode = "404", description = "Template not found")
    public TemplateConfig getTemplate(
            @Parameter(description = "Template ID") @PathVariable String id) {
        return templateConfigService.getTemplateConfigById(id);
    }

    private void validateListeners(TemplateConfig templateConfig) {
        if (templateConfig.getListeners() != null) {
            for (ListenerConfig listener : templateConfig.getListeners()) {
                if (listener.getType() == null || listener.getTarget() == null) {
                    throw new IllegalArgumentException("Listener configuration must include type and target");
                }
                if (!"HTTP".equalsIgnoreCase(listener.getType()) && !"CODE".equalsIgnoreCase(listener.getType())) {
                    throw new IllegalArgumentException("Listener type must be either HTTP or CODE");
                }
            }
        }
    }
}
